package main

import (
	"fmt"
	"os"
	"log"
	"math/rand"
	"container/list"
	"sync"
	"unsafe"
	"crypto/sha1"
	"encoding/json"
	"time"
)

var Per_value uint64 = 1000;


/**
返回类型
 */
func run(arg string) (string, int) {
	fmt.Println(arg);
	return "返回值", 100;
}

type IFly interface {
	Fly();
}

type Brid struct {

}
//实现接口IFly
func (Brid) Fly() {
	fmt.Println("接口测试!!!");
}


/***
chan ---
 for(value: values){

 }
 */
func sum(values [] int, resultChan chan int) {
	sum := 0
	for index, value := range values {
		sum += value

		fmt.Printf("%d,value:%d\n", index, value);
	}

	resultChan <- sum // 将计算结果发送到channel中
	temp := <-resultChan; //  callback
	resultChan <- temp; //
	fmt.Printf("temp:%d", temp); //
}

/**变量名称大写  包外可以访问**/
const (
	Sunday = iota
	Monday
	Tuesday
	Wednesday
	Thursday
	Friday
	Saturday
	numberOfDays
)
// PersonInfo 一         的
// int
type PersonInfo struct {
	ID      int
	Name    string
	Address string
}

func main() {

	var _person_map map[string]PersonInfo = make(map[string]PersonInfo, 10);
	_person_map["1"] = PersonInfo{89, "石头哥哥", "重庆"}

	delete(_person_map, "0")

	//for index, value := range list.New() {
	//	if index=target_index {
	//
	//	}
	//}


	/**
		status  进行判断是否存在指定值
	 */
	a_persion, status := _person_map["2"]; //
	if status {
		fmt.Printf("found\n");
		fmt.Println(a_persion.Address + ":" + string(a_persion.ID) + ":" + a_persion.Name)
	} else {
		_json_, _ := json.Marshal(a_persion); //[]byte ----jsonstring
		fmt.Printf("@@%v ,%d\n", string(_json_), status);
	}

	_channel := make(chan int)

	go func() {
		for {
			time.Sleep(time.Second * 1)
			_channel <- 78
		}
	}()//跟参数列表,表示直接调用


	res:=func(x, y int) int {
		z := x * y%10;
		fmt.Printf("匿名函数测试:%d\n", z);
		return z;
	}(54, 997)

	fmt.Printf("匿名函数测试返回结果:%d\n", res);



	//
	select {
	case i := <-_channel:
		fmt.Println(i)
	}

	a := []int{1, 2, 3, 4, 5, 6}
	fmt.Printf("base:%v\n", a);
	f_modiy := func(_array[]int) {
		_array[0] = 100
		fmt.Printf("f_modiy :%v\n", a);
	}
	f_modiy(a); //匿名函数
	fmt.Printf("%v\n", a);


	/**
	range : 返回1个值
	第一个返回值: 索引
	第二个返回值: 返回集合中的元素
	 */

	_test_array := []uint32{10, 89, 99};
	myslice := _test_array[:]
	for index, value := range myslice {
		fmt.Printf("index:%d,value:%d\n", index, value)
	}

	values := [11] int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	resultChan := make(chan int, 2)
	var _size uintptr = unsafe.Sizeof(resultChan);
	fmt.Printf("size:%d\n", _size)
	fmt.Printf("len:%d\n", len(resultChan))



	//切片
	go sum(values[0:len(values) / 2], resultChan)
	go sum(values[len(values) / 2:len(values)], resultChan)

	/***/
	_hash := sha1.New();
	println(_hash.BlockSize())

	sum1, _ok := <-resultChan
	sum2, _ok2 := <-resultChan

	//time.Sleep(1);
	if (_ok&&_ok2) {
		fmt.Println("Result:", sum1, sum2, sum1 + sum2)
		//close(resultChan)
	}

	var _fly IFly = new(Brid);
	_fly.Fly();


	list.New();
	fmt.Printf("随机数:%f\n", rand.Float32());
	value := 1000;
	fmt.Printf("测试:%d\n", value)
	fmt.Println("测试!!!!!")

	arg := "这是测试string";
	//fmt.Println(arg);
	_, __value := run(arg); // 赋值
	log.Printf("--%d\n", __value);
	if arg == "" {
		/**
		打开文件
		 */
		fd, err := os.Open("");
		if err != nil {
			log.Println("Open file failed:", err)
			return
		}
		defer fd.Close()
	}

	/**
	匿名函数  闭包
	 */

	f := func(x, y uint32) uint32 {
		return x + y
	}

	fmt.Printf("%d\n", f(100, 11))

	{

		var lock sync.RWMutex;
		//var _cond sync.Cond
		//    c.L.Lock()
		//    for !condition() {
		//        c.Wait()
		//    }
		//    ... make use of condition ...
		//    c.L.Unlock()
		//
		lock.Lock()
		go run("协程测试!!!")
		defer lock.Unlock()
	}

	var _v_map map[int]int
	_v_map = make(map[int]int, 10);
	fmt.Println(_v_map);

	_v_map[1] = 10000
	map_value, ok := _v_map[1]; // first is map value ,second is status
	if ok {
		fmt.Printf("map value%d,%v\n", map_value, ok)
	}


	//变量初始化
	init_value := 100;
	fmt.Printf("init_value:%d\n", init_value);
	//变量赋值
	var v1 int;
	v1 = 100;
	fmt.Printf("init_value:%d\n", v1);

	//编译期行为
	const vv0, name = 100, "石头哥哥"
	fmt.Printf("%v\n", name);

	const (
		c0 = iota  // 0
		c1                //1
		c2                //2
	)


	defer func() {
		fmt.Print("测试defer!!!!\n")
	}()


}
